package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysStudentMapper;
import com.ruoyi.system.domain.SysStudent;
import com.ruoyi.system.service.ISysStudentService;
import com.ruoyi.common.core.text.Convert;

/**
 * 学生Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-14
 */
@Service
public class SysStudentServiceImpl implements ISysStudentService 
{
    @Autowired
    private SysStudentMapper sysStudentMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询学生
     * 
     * @param studentId 学生主键
     * @return 学生
     */
    @Override
    public SysStudent selectSysStudentByStudentId(String studentId)
    {
        return sysStudentMapper.selectSysStudentByStudentId(studentId);
    }

    /**
     * 查询学生列表
     * 
     * @param sysStudent 学生
     * @return 学生
     */
    @Override
    public List<SysStudent> selectSysStudentList(SysStudent sysStudent)
    {
        return sysStudentMapper.selectSysStudentList(sysStudent);
    }

    /**
     * 新增学生
     * 
     * @param sysStudent 学生
     * @return 结果
     */
    @Override
    public int insertSysStudent(SysStudent sysStudent)
    {
        sysStudent.setStudentId(IdUtils.fastUUID());
        sysStudent.setCreateTime(DateUtils.getNowDate());
        SysUser user = new SysUser();
        user.setLoginName(sysStudent.getNumber());
        user.setUserName(sysStudent.getName());
        user.setPassword(sysStudent.getPassword());
        user.setSalt(sysStudent.getSalt());
        user.setDeptId(Long.valueOf(sysStudent.getDeptId()));
        Long[] roleIds = new Long[1];
        roleIds[0] = 4L;
        user.setRoleIds(roleIds);
       int userId=userService.addUser(user);
        if(userId>0){
            sysStudent.setUserId(userId+"");
           return  sysStudentMapper.insertSysStudent(sysStudent);
        }
        return 0;
    }

    /**
     * 修改学生
     * 
     * @param sysStudent 学生
     * @return 结果
     */
    @Override
    public int updateSysStudent(SysStudent sysStudent)
    {
        sysStudent.setUpdateTime(DateUtils.getNowDate());
        return sysStudentMapper.updateSysStudent(sysStudent);
    }

    /**
     * 批量删除学生
     * 
     * @param studentIds 需要删除的学生主键
     * @return 结果
     */
    @Override
    public int deleteSysStudentByStudentIds(String studentIds)
    {
        return sysStudentMapper.deleteSysStudentByStudentIds(Convert.toStrArray(studentIds));
    }

    /**
     * 删除学生信息
     * 
     * @param studentId 学生主键
     * @return 结果
     */
    @Override
    public int deleteSysStudentByStudentId(String studentId)
    {
        return sysStudentMapper.deleteSysStudentByStudentId(studentId);
    }
}
